<template>
    <div class="count">
        <div class="topTit f f-a-c f-j-b">
            <div class="left f f-a-c">
                <img src="/src/status/img/logo.png" class="logo" alt="">
                <span>H.S.国际问题研究平台</span>
            </div>
            <div class="right">
                <img @click="setting" src="/src/status/img/geng.png" class="myStat" alt="">
            </div>
        </div>
        <div class="body">
            <!-- 热点时间 -->
            <div class="hotSpotTime f f-a-c f-j-b">
                <div class="hot">热点事件</div>
                <div @click="show = true" class="Settings f f-a-c">
                    <img src="/src/status/colorJia.png" class="set" alt="">
                    <div class="text">订阅设置</div>
                </div>
            </div>
            <!-- tabbar切换 -->
            <div class="tab f f-a-c">
                <div class="one" @click="tabs(index)" v-for="(item, index) in valueList" :key="item.id"
                    :class="{ act: statis == index }">{{ item.zh_name }}</div>
                <!-- <div class="one" @click="tabs(1)" :class="{ act: statis == 1 }">美国</div>
                <div class="one" @click="tabs(2)" :class="{ act: statis == 2 }">俄罗斯</div> -->
            </div>
            <!-- 这个是列表 -->
            <div class="list">
                <van-list offset="0" @load="onLoad">
                    <div v-for="(item, index) in lists" @click="goDetilTo(item)" class="listItem f f-a-c f-j-b"
                        :key="index">
                        <img :src="item.image" class="shoopImg" alt="">
                        <div class="right f f-d-c">
                            <div class="tite">{{ item.zh_title || item.title }}</div>
                            <div class="time">{{ item.news_time }}</div>
                        </div>
                    </div>
                </van-list>
            </div>
        </div>
        <!-- 订阅设置 -->
        <van-action-sheet v-model:show="show">
            <div class="content">
                <div class="top">订阅设置</div>
                <div class="tips">设置要订阅的国家，确定后下次进入时生效（最多可添加4个国家）</div>
                <!-- 下面的四个列表 -->
                <div class="nation">
                    <div v-for="(item, index) in valueList" :key="index" class="cheas f f-a-c f-j-b">
                        <img src="/src/status/jian.png" @click="deteleNat(item, index)" class="jian" alt="">
                        <div class="box f f-a-c f-j-b">
                            <input type="text" @click="setleFn(index)" v-model="item.zh_name" placeholder="请选择国家"
                                class="names" name="" id="">
                            <img src="/src/status/jiantou.png" class="tubiao" alt="">
                        </div>
                    </div>
                    <div v-show="addShow" @click="addnation" class="nav">
                        <div class="addGuo f f-a-c">
                            <img src="/src/status/jia.png" class="jia" alt="">
                            添加国家
                        </div>
                    </div>
                    <!-- 这个是下方的确认 -->
                    <div class="isOk" @click="isOk">确定</div>
                </div>
            </div>
        </van-action-sheet>
        <!-- 地址 选择弹出层 -->
        <van-field v-model="fieldValue" is-link readonly label="城市" placeholder="选择城市" @click="showPicker = true" />
        <van-popup v-model:show="showPicker" round position="bottom">
            <van-picker :columns="columns" :columns-field-names="customFieldName" @cancel="showPicker = false"
                @confirm="onConfirm" />
        </van-popup>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import router from '../../router';
import { GetHotSpotList, addressUri, ModifyWikiUriIds, GetSubscribeCountry } from '../../api/index'
import { showToast, showLoadingToast, closeToast, showConfirmDialog } from 'vant'
import { number } from 'echarts';

// 地址数组
let columns = ref([
    { zh_name: '杭州', value: 'Hangzhou' },
    { zh_name: '宁波', value: 'Ningbo' },
    { zh_name: '温州', value: 'Wenzhou' },
    { zh_name: '绍兴', value: 'Shaoxing' },
    { zh_name: '湖州', value: 'Huzhou' },
]);
let num = ref(0)
let page = ref(0)
let times = ref(null)
let max_id = ref(0)
let customFieldName = ref({
    text: 'zh_name',
    value: 'id'
})
const fieldValue = ref('');
const showPicker = ref(false);
const show = ref(false);
let statis = ref(0)
const addShow = ref(true);
// vant组件列表
const list = ref([]);
const lists = ref([]);
const valueList = ref([{
    zh_name: '',
    id: 1
}]);
// 这个是获取国家
onMounted(async () => {

    let n = await addressUri()
    columns.value = n.content.list
    // 调用我的订阅列表
    mySubscription()

})
// 获取我的订阅列表
const mySubscription = async () => {
    let n = await GetSubscribeCountry()
    valueList.value = n.content.list
    // 这个是列表数据
    // homeList()
}

// 点击确认
const isOk = async () => {
    let str = []
    valueList.value.forEach(item => {
        str.push(item.id)
    })
    // console.log(str.join(','), '12215');
    let n = await ModifyWikiUriIds({
        wiki_uri_ids: str.join(',')
    })
    showToast(n.msg)
}

// 首页的列表
const homeList = async () => {
    const toast = showLoadingToast({
        duration: 0,
        forbidClick: true,
        message: '加载中',
    });
    let n = await GetHotSpotList({
        wiki_uri_id: valueList.value[statis.value].id,
        page: page.value,
        pageSize: 10,
        max_id: page.value == 1 ? '' : max_id.value
    })
    max_id.value = n.content.list[0].id
    lists.value = [...lists.value, ...n.content.list]
    closeToast();
    console.log(lists.value, '000000000000000');
}
// 这个是地址选择确定
const onConfirm = ({ selectedOptions }) => {
    valueList.value[num.value] = selectedOptions[0]
    showPicker.value = false;
    console.log(valueList.value, '222020');
};
// 点击打开选择地址
const setleFn = (index) => {
    num.value = index
    showPicker.value = true
}
// 点击跳转到详情
const goDetilTo = (item) => {
    router.push({
        path: '/EventDetailss',
        query: {
            id: 1,
            spot_id: item.id
        }
    })
}
const onLoad = (e) => {
    console.log(e, '123');

    clearTimeout(times.value)
    times.value = setTimeout(() => {
        // page.value = Number(page.value) + 1
        page.value++
        homeList()
    }, 500)
};
// 点击切换ttabs
const tabs = (num) => {
    statis.value = num
    page.value = 1
    lists.value = []
    homeList()
}
// 点击添加国家
const addnation = () => {
    if (valueList.value.length < 4) {
        if (valueList.value.length == 3) {
            valueList.value.push({ zh_name: '', id: valueList.value.length + 1 })
            addShow.value = false
            return
        }
        valueList.value.push({ zh_name: '', id: valueList.value.length + 1 })
    }

}
// 点击删除国家
const deteleNat = (item, index) => {
    valueList.value.splice(index, 1)
    if (valueList.value.length < 4) addShow.value = true

    console.log(valueList.value, '222020');
}
// 跳转到我的模块
const setting = () => {
    router.push('/setting')
}
</script>
<style lang="scss" scoped>
@function pxfn($px) {
    @return calc($px / 3.75) * 1vw;
}

.nav {
    display: flex;
    align-items: center;
    justify-content: flex-end;

    .addGuo {
        width: 90%;
        height: pxfn(40);
        border: pxfn(1) solid #32BBEA;
        padding: 0 pxfn(15);
        border-radius: pxfn(6);
        font-size: pxfn(14);
        font-family: PingFang SC, PingFang SC-400;
        font-weight: 400;
        text-align: LEFT;
        color: #32bbea;

        .jia {
            width: pxfn(16);
            height: pxfn(16);
            margin-right: pxfn(6);
        }
    }
}

.nation {
    padding-bottom: pxfn(70);

    .isOk {
        width: 90%;
        height: pxfn(44);
        line-height: pxfn(44);
        background: #32bbea;
        border-radius: pxfn(6);
        position: fixed;
        bottom: pxfn(10);
        left: 50%;
        transform: translateX(-50%);
        font-size: pxfn(14);
        font-family: PingFang SC, PingFang SC-500;
        font-weight: 500;
        text-align: center;
        color: #ffffff;
    }

    .cheas {
        margin-bottom: pxfn(10);

        .jian {
            width: pxfn(16);
            height: pxfn(16);
        }

        .box {
            position: relative;
            width: 90%;
            height: pxfn(40);
            border: pxfn(1) solid #e8eaef;
            border-radius: pxfn(6);
            padding: 0 pxfn(15);

            .names {
                font-size: pxfn(14);
                font-family: PingFang SC, PingFang SC-400;
                font-weight: 400;
                text-align: LEFT;
                color: #1d2129;
                width: 100%;
                height: 100%;
                border: none;
            }

            .tubiao {
                width: pxfn(10);
                height: pxfn(10);
            }
        }
    }
}

.content {
    padding: pxfn(15) pxfn(15) pxfn(60) pxfn(15);
    max-height: 80vh;
    overflow-y: auto;

    .top {
        font-size: pxfn(20);
        font-family: PingFang SC, PingFang SC-600;
        font-weight: 600;
        text-align: LEFT;
        color: #1d2129;
        margin-bottom: pxfn(15);
    }

    .tips {
        font-size: pxfn(14);
        font-family: PingFang SC, PingFang SC-400;
        font-weight: 400;
        text-align: LEFT;
        color: #868d9c;
        margin-bottom: pxfn(10);
    }
}

.list {
    padding: pxfn(15);

    .listItem {
        width: pxfn(343);
        height: pxfn(112);
        border-bottom: pxfn(1) solid #edf3fa;

        .shoopImg {
            width: pxfn(120) !important;
            height: pxfn(80);
            border-radius: pxfn(8);
            margin-right: pxfn(15);
        }

        .right {
            width: pxfn(200);
        }

        .tite {
            font-size: pxfn(14);
            font-family: PingFang SC, PingFang SC-500;
            font-weight: 500;
            color: #1d2129;
            text-overflow: -o-ellipsis-lastline;
            overflow: hidden; //溢出内容隐藏
            text-overflow: ellipsis; //文本溢出部分用省略号表示
            display: -webkit-box; //特别显示模式
            -webkit-line-clamp: 2; //行数
            line-clamp: 2;
            -webkit-box-orient: vertical;
        }

        .time {
            font-size: pxfn(12);
            font-family: PingFang SC, PingFang SC-400;
            font-weight: 400;
            text-align: LEFT;
            color: #868d9c;
            margin-top: pxfn(20);
        }
    }
}

.tab {
    padding: 0 pxfn(15);
    // background-color: pink;
    width: 100%;
    overflow-x: auto;

    .one {
        padding: pxfn(4) pxfn(6);
        border: pxfn(1) solid #e8eaef;
        border-radius: pxfn(6);
        font-size: pxfn(14);
        font-family: PingFang SC, PingFang SC-400;
        font-weight: 400;
        text-align: center;
        color: #1d2129;
        margin-right: pxfn(10);
    }

    .act {
        border: pxfn(1) solid #32bbea;
        color: #32BBEA;
    }
}

.body {
    height: calc(100vh - pxfn(48));

}

.list {
    margin-top: pxfn(10);
    height: calc(100vh - pxfn(150));
    // background-color: pink;
    overflow-y: auto;
}

.hotSpotTime {
    padding: pxfn(15);

    .hot {
        // background: #1d2129;
        font-size: pxfn(20);
        font-family: PingFang SC, PingFang SC-600;
        font-weight: 600;
        text-align: LEFT;
        color: #1d2129;
    }

    .Settings {
        .set {
            width: pxfn(15);
            height: pxfn(15);
            margin-right: pxfn(5);
        }

        .text {
            font-size: pxfn(14);
            font-family: PingFang SC, PingFang SC-400;
            font-weight: 400;
            text-align: LEFT;
            color: #32bbea;
        }
    }
}

.count {
    width: 100vw;
    height: 100vh;
    background-color: #fff;
    padding-top: pxfn(50);
}

.topTit {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: pxfn(48);
    border: 1px solid #edf3fa;
    padding: pxfn(15);
    z-index: 1;
    font-size: pxfn(14);
    font-weight: 600;

    .logo {
        width: pxfn(27);
        height: pxfn(24);
        margin-right: pxfn(10);
    }

    .myStat {
        width: pxfn(24);
        height: pxfn(24);
    }
}
</style>